﻿//2013-11-27 21:45:41 U 待优化,不要与系统相关联.
document.write('<scr' + 'ipt type="text/javascript" src="/Scripts/ajaxupload.3.5.js"></scr' + 'ipt>');
(function ($) {
    $.fn.initUpload = function (options) {
        var defaults = {
            savePath: '',
            maxSize: 1000000,
            Expand: 'jpg|png|jpeg|gif|css',
            thumScale: '100x100_HW',
            actLink: '/Controllers/Ashx/UploadFile.ashx'
        }

        var options = $.extend(defaults, options);

        this.each(function () {
            var btn = $(this);

            function render() {
                var $m = $.messager;

                new AjaxUpload(btn, {
                    action: options.actLink + '?p=' + options.savePath + '&maxlen=' + options.maxSize + '&thumscale=' + options.thumScale,
                    name: 'imgFile',
                    onSubmit: function (file, ext) {
                        if (!(ext && (new RegExp(options.Expand)).test(ext))) {
                            if ($m)
                                $m.alert('提示', '只能上传 ' + options.Expand + ' 类型的文件！', 'info');
                            else
                                alert('只能上传 ' + options.Expand + ' 类型的文件！');

                            return false;
                        }

                        if ($m)
                            $m.progress({ title: '提示', msg: '正在上传...', interval: 500 });
                    },
                    onComplete: function (file, response) {
                        if ($m)
                            $m.progress('close');

                        response = eval("(" + response + ")");

                        if (response.error != "0") {
                            if ($m)
                                $m.alert('提示', '上传失败!', 'error');
                            else
                                alert('上传失败!');
                        } else if (response.error.toString().indexOf('limit') > 0) {
                            if ($m)
                                $m.alert('错误提示', '文件大小不得超过' + options.maxSize + 'kb！', 'info');
                            else
                                alert('文件大小不得超过' + options.maxSize + 'kb！')
                        }
                        else {
                            if (options.showID) {
                                $('#' + options.showID).attr('src', response.url);
                            }
                            if (options.hideID) {
                                $('#' + options.hideID).val(response.url);
                            }
                            if ($m) {
                                $m.progress('close');
                                $m.alert('温馨提示', '上传成功!', 'info');
                            }
                            else {
                                alert('上传成功!')
                            }
                        }
                    }
                });
            }
            render();
        });
    }
})(jQuery);